--[[
	Copyright (c) 2012 Eyal Shilony

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

local addonName, addon = ...
local options = addon:NewModule("Options")
local commando = LibStub("Commando-1.0")
local L = LibStub("AceLocale-3.0"):GetLocale(addonName)

options:RegisterEvent("ADDON_ENABLED", function(self, even)
	local options = {
		name = addon.name,
		type = "group",
		args = {
			{
				type = "description",
				name = function()
					return GetAddOnMetadata(addonName, "Notes")
				end,
				cmdHidden = true,
			},
			{
				type = "header",
				name = "",
			},
			{
				type = "description",
				name = "",
			},
			{
				type = "toggle",
				name = L["Block the menu in combat"],
				get = function()
					return addon.db.profile.blockInCombat
				end,
				set = function()
					addon.db.profile.blockInCombat = not addon.db.profile.blockInCombat
				end,
				width = "full",
			},
			{
				type = "toggle",
				name = L["Hide the logout and exit items from the menu"],
				get = function()
					return addon.db.profile.hideLogoutButtons
				end,
				set = function()
					addon.db.profile.hideLogoutButtons = not addon.db.profile.hideLogoutButtons
					addon.updateMenu = true
				end,
				width = "full",
			},
			{
				type = "toggle",
				name = L["Hide the reload item from the menu"],
				get = function()
					return addon.db.profile.hideReloadUIButton
				end,
				set = function()
					addon.db.profile.hideReloadUIButton = not addon.db.profile.hideReloadUIButton
					addon.updateMenu = true
				end,
				width = "full",
			},
			{
				type = "toggle",
				name = L["Hide the options item from the menu"],
				get = function()
					return addon.db.profile.hideOptionsButton
				end,
				set = function()
					addon.db.profile.hideOptionsButton = not addon.db.profile.hideOptionsButton
					addon.updateMenu = true
				end,
				width = "full",
			},
			{
				type = "header",
				name = L["Broker Display Pattern"],
			},
			{
				type = "description",
				name = L["Customize the broker display using plain text and the tags below."],
			},
			{
				type = "description",
				name = "",

			},
			{
				type = "description",
				name = L["@addons: the total memory that addons consume.\n@bags: the number of available bag slots out of the total slots.\n@calendar: the number of calendar invitations to which the player has yet to respond.\n@fps: the current fps.\n@friends: The number of friends online.\n@guild: the number of online guild members out of the total members.\n@latency: the current latency.\n@money: the amount of money.\n@position: the current position of the player.\n@time: the current time."],
			},
			{
				type = "input",
				name = "",
				get = function()
					return addon.db.profile.display
				end,
				set = function(_, value)
					addon.db.profile.display = value
				end,
				width = "full",
			},
			{
				type = "description",
				name = "",
			},
			{
				type = "header",
				name = L["Tooltip"],
			},
			{
				type = "description",
				name = "",
			},
			{
				type = "toggle",
				name = L["Hide tooltip"],
				get = function()
					return addon.db.profile.hideTooltip
				end,
				set = function()
					addon.db.profile.hideTooltip = not addon.db.profile.hideTooltip
				end,
				width = "full",
			},
			{
				type = "toggle",
				name = L["Hide the accessibility bindings"],
				get = function()
					return addon.db.profile.hideTooltipAccessibilityBindings
				end,
				set = function()
					addon.db.profile.hideTooltipAccessibilityBindings = not addon.db.profile.hideTooltipAccessibilityBindings
				end,
				width = "full",
			},
			{
				type = "range",
				name = L["Number of addons to display"],
				get = function()
					return addon.db.profile.numAddOns
				end,
				set = function(_, value)
					addon.db.profile.numAddOns = value
				end,
				step = 1,
				min = 1,
				max = 40,
				width = "full",
			},
		},
	}
	addon:RegisterOptions(options)
	
	local accessibility = {
		name = L["Accessibility"],
		type = "group",
		args = {
			{
				type = "description",
				name = L["Bind one of the mouse buttons to a menu item and run it by clicking on the broker display."],
				cmdHidden = true,
			},
			{
				type = "header",
				name = "",
			},
			{
				type = "select",
				name = L["Mouse Setting"],
				get = function()
					return addon.db.profile.mouseButton
				end,
				set = function(_, value)
					addon.db.profile.mouseButton = value
				end,
				values = addon.buttons,
			},
			{
				type = "header",
				name = "",
			},
		},
	}
	
	local num = 0
	local titles = { }
	for _, v in commando:Actions() do
		num = num + 1
		table.insert(titles, v.title)
	end
	
	for i, v in pairs(addon.db.profile.accessibility) do
		table.insert(accessibility.args, {
			type = "select",
			name = L["Action %s"]:format(i),
			get = function()
				local _, index = commando:GetAction(addon.db.profile.accessibility[i].name)
				return index
			end,
			set = function(_, value)
				local action = commando:GetActionByIndex(value)
				addon.db.profile.accessibility[i].name = action.name
			end,
			values = titles,
			width = "double",
		})
		table.insert(accessibility.args, {
			type = "select",
			name = L["Modifier"],
			get = function()
				return addon.db.profile.accessibility[i].modifier
			end,
			set = function(_, value)
				addon.db.profile.accessibility[i].modifier = value
			end,
			values = addon.modifiers,
		})
	end
	addon:RegisterOptions(accessibility)
	
	local order = {
		name = L["Order"] ,
		type = "group",
		args = {
			{
				type = "description",
				name = L["Sort the order of the menu items.\n\nNote, having no sorting order (zero) hides the item."],
				cmdHidden = true,
			},
			{
				type = "header",
				name = "",
			},
		},
	}
	
	for _, v in commando:Actions() do
		table.insert(order.args, {
			type = "range",
			name = v.title,
			get = function()
				return addon.db.profile.order[v.name]
			end,
			set = function(_, value)
				addon.db.profile.order[v.name] = value
				addon.updateMenu = true
			end,
			step = 1,
			min = 0,
			max = num,
			width = "full",
		})
	end
	addon:RegisterOptions(order)
	
	addon:RegisterOptions(LibStub("AceDBOptions-3.0"):GetOptionsTable(addon.db), true)
end)
